package org.ling.xuan.pannel;

import com.intellij.openapi.project.Project;
import com.intellij.ui.components.JBLabel;
import com.intellij.ui.components.JBPanel;
import com.intellij.ui.components.JBTextField;
import org.ling.xuan.entity.mo.ProjectSetting;

import java.awt.*;

import static org.ling.xuan.entity.mo.ProjectSetting.initProjectSettingByProject;

public class ProjectSettingPanel extends JBPanel {
    private ProjectSetting projectSetting;
    private final JBTextField localPreField;
    private final JBTextField svnPreField;
    private final JBTextField addPreField;
    private final JBTextField zipPreField;
    private final JBTextField compilePathField;
    private final JBTextField testCompilePathField;
    private final JBTextField buildPathField;
    private final JBTextField projectPathField;
    private final JBTextField webPathField;
    private final JBTextField unionCommitPath;
    private final JBTextField projectNameField;
    private final JBTextField systemNameField;

    public ProjectSettingPanel(Project project, ProjectSetting ps) {
        this.projectSetting = ps;
        if (this.projectSetting == null) {
            this.projectSetting = initProjectSettingByProject(project);
        }
        setLayout(new GridLayout(12, 2, 10, 10));
        localPreField = new JBTextField(200);
        localPreField.setToolTipText("添加时,会把文件路径的本地前缀替换成添加前缀");
        svnPreField = new JBTextField(200);
        svnPreField.setToolTipText("当前项目根目录所对应的svn-httpUrl");
        addPreField = new JBTextField(200);
        localPreField.setToolTipText("添加前缀");
        zipPreField = new JBTextField(200);
        zipPreField.setToolTipText("压缩前缀,打包压缩修改文件用,相对于添加前缀的路径的相对路径.例如:添加前缀是/Sources/app/ifmpub," +
                "压缩前缀是/Sources/app,那么意思是在/Sources/app进行压缩");
        compilePathField = new JBTextField(200);
        testCompilePathField = new JBTextField(200);
        buildPathField = new JBTextField(200);
        projectPathField = new JBTextField(200);
        webPathField = new JBTextField(200);
        unionCommitPath = new JBTextField(200);
        unionCommitPath.setToolTipText("统一提交路径,多项目svn提交时用,统一提交路径相同,则合并成一次提交.如果配置为空,则提交只在当前项目提交.可能产生多次提交记录,确保该路径存在.svn文件夹");
        projectNameField = new JBTextField(20);
        systemNameField = new JBTextField(20);
        add(new JBLabel("本地前缀:"));
        add(localPreField);
        add(new JBLabel("svn下载前缀:"));
        add(svnPreField);
        add(new JBLabel("添加前缀:"));
        add(addPreField);
        add(new JBLabel("压缩前缀:"));
        add(zipPreField);
        add(new JBLabel("编译目录:"));
        add(compilePathField);
        add(new JBLabel("测试编译目录:"));
        add(testCompilePathField);
        add(new JBLabel("打包目录:"));
        add(buildPathField);
        add(new JBLabel("项目根路径:"));
        projectPathField.setEnabled(false);
        add(projectPathField);
        add(new JBLabel("web目录:"));
        add(webPathField);
        add(new JBLabel("统一提交路径:"));
        add(unionCommitPath);
        add(new JBLabel("项目名称:"));
        add(projectNameField);
        add(new JBLabel("系统名称:"));
        add(systemNameField);
        projectNameField.setEnabled(false);
        localPreField.setText(projectSetting.getLocalPre());
        svnPreField.setText(projectSetting.getSvnPre());
        addPreField.setText(projectSetting.getAddPre());
        zipPreField.setText(projectSetting.getZipPre());
        compilePathField.setText(projectSetting.getCompilePath());
        testCompilePathField.setText(projectSetting.getTestCompilePath());
        buildPathField.setText(projectSetting.getBuildPath());
        projectPathField.setText(projectSetting.getProjectPath());
        webPathField.setText(projectSetting.getWebPath());
        projectNameField.setText(projectSetting.getProjectName());
        systemNameField.setText(projectSetting.getSystemName());
        unionCommitPath.setText(projectSetting.getUnionCommitPath());
    }


    public ProjectSetting getSetting() {
        projectSetting.setLocalPre(localPreField.getText());
        projectSetting.setSvnPre(svnPreField.getText());
        projectSetting.setAddPre(addPreField.getText());
        projectSetting.setZipPre(zipPreField.getText());
        projectSetting.setCompilePath(compilePathField.getText());
        projectSetting.setTestCompilePath(testCompilePathField.getText());
        projectSetting.setBuildPath(buildPathField.getText());
        projectSetting.setProjectPath(projectPathField.getText());
        projectSetting.setWebPath(webPathField.getText());
        projectSetting.setProjectName(projectNameField.getText());
        projectSetting.setSystemName(systemNameField.getText());
        projectSetting.setUnionCommitPath(unionCommitPath.getText());
        return projectSetting;
    }
}
